package lk.mrt.cse.wihidum.router.processor;

import org.apache.log4j.Logger;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.AxisFault;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;

import javax.xml.namespace.QName;
import java.util.Iterator;

public class GetPublicationsProcessor {

    private static final Logger logger = Logger.getLogger (GetPublicationsProcessor.class);

    public String getPublications(String subscriptionID, String subscriberAddress, String listenerAddress) {
        try
        {
            ServiceClient serviceClient = new ServiceClient();
            Options options = new Options();
            options.setTo(new EndpointReference(subscriberAddress));
            options.setAction("getPublications");
            serviceClient.setOptions(options);

            OMFactory factory = OMAbstractFactory.getOMFactory();

            OMElement messageElement = factory.createOMElement("GetPublicationsRequest", null);

            OMElement subscriptionHeader = factory.createOMElement("Header", null);
            OMElement actionElement = factory.createOMElement("Action", null);
            actionElement.setText("getPublications");
            subscriptionHeader.addChild(actionElement);
            messageElement.addChild(subscriptionHeader);

            OMElement subscriptionIDElement = factory.createOMElement("subscriptionID", null);
            subscriptionIDElement.setText(subscriptionID);
            messageElement.addChild(subscriptionIDElement);

            OMElement listenerAddressElement = factory.createOMElement("listenerAddress", null);
            listenerAddressElement.setText(listenerAddress);
            messageElement.addChild(listenerAddressElement);

            OMElement responseElement = serviceClient.sendReceive(messageElement);

            OMElement referenceParameters = responseElement.getFirstChildWithName(new QName("ReferenceParameters"));
            OMElement responseTextElement = referenceParameters.getFirstChildWithName(new QName("Response"));

            String data = "";
            for (Iterator<OMElement> publicationElement = responseTextElement.getChildrenWithName(new QName("Publication")); publicationElement.hasNext();)
            {
                OMElement nextPublication = publicationElement.next();
                OMElement publisher = nextPublication.getFirstChildWithName(new QName("publisherAddress"));
                OMElement topic = nextPublication.getFirstChildWithName(new QName("topic"));
                OMElement publicationText = nextPublication.getFirstChildWithName(new QName("publicationText"));

                if (data.isEmpty()) {
                    data = topic.getText() + "#" + publicationText.getText();
                }
                else {
                    data = data + "@" + topic.getText() + "#" + publicationText.getText();
                }
            }
            return data;
        }
        catch (NullPointerException ex)
        {
            return "Error in processing get publications response";
        }
        catch (AxisFault ex)
        {
            return "Error in sending get publications request";
        }
    }
}
